<?php

namespace App\Services;

use App\Dao\ShopsDao;
use App\Exceptions\SysExceptions;
use Carbon\Carbon;
use Psr\SimpleCache\InvalidArgumentException;

class DouFuServices
{

    private static $appId = 'awaervbxmlkcyrr2';
    private static $appSecret = '912a9bf3d683465a5ee79a342cf5a0a1';

    /**
     * @throws SysExceptions
     * @throws InvalidArgumentException
     */
    public static function shopAutoTongBu(){
        $uri = 'https://open.douyin.com/goodlife/v1/shop/poi/query/?page=1&size=20';
        $token = self::get_client_token();
        $datas = self::httpcurls($uri,[],0,[
            'Content-Type'=>'application/json',
            'access-token'=>$token
        ]);
        print_r($datas);
    }



    public static function shop_add($params = []){
        $uri = 'https://open.douyin.com/goodlife/v1/partner/order/create/';
        $token = self::get_client_token();
        $data = [
            'account_id'=>$params['account_id'],
            'cooperation_content'=>5,
            'start_time'=>time()+200,
            'end_time'=>Carbon::now()->addYears(1)->timestamp-86400,
            'charge_type'=>$params['shop_type'],
        ];
        //整体抽佣
        if($data['charge_type'] == 1){
            $data['commission_ratio'] = (string)($params['shop_value_yong']*100);
        }
        $header = array(
            'Content-Type:application/json',
            'access-token:'.$token
        );
        $datas = self::httpcurls($uri,$header,json_encode($data,JSON_UNESCAPED_UNICODE));
        if($datas['data']['error_code'] != 0){
            throw new SysExceptions($datas['data']['description']);
        }
        ShopsDao::get_builder_query([
            'shop_laike_id'=>$data['account_id']
        ])->update([
            'status'=>102
        ]);
    }







    /**
     * @throws SysExceptions
     * @throws InvalidArgumentException
     */
    private static function get_client_token(){
        if(Cache::hasCache('client_token_douyin')){
            return  Cache::getCacheValue('client_token_douyin');
        }
        $uri = 'https://open.douyin.com/oauth/client_token/';
        $datas = self::httpcurls($uri,[
            'Content-Type'=>'application/json'
        ],['client_key'=>self::$appId,
            'client_secret'=>self::$appSecret,
            'grant_type'=>'client_credential'
        ]);

        if($datas['data']['error_code'] != 0){
            throw new SysExceptions($datas['message']);
        }
        Cache::setCache('client_token_douyin',$datas['data']['access_token'],4000);
        return $datas['data']['access_token'];
    }

    public static function httpcurls($sUrl, $aHeader, $aData)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_URL, $sUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $aData);
        $sResult = curl_exec($ch);
        if($sError=curl_error($ch)){
            die($sError);
        }
        curl_close($ch);
        return json_decode($sResult,true);
    }

}
